<?php
$version = Zend_Registry::get('version');  
$menu_db_class = new Menu_Model_DbTable_Menu(); $menu_name = ($this->menu_id)? $menu_db_class->getMenuName($this->menu_id) : 'Frontend Login';  
$this->headTitle()->headTitle($menu_name,'SET');

$returnLink = Eicra_Global_Variable::getSession()->returnLink;
$redirect_msg = $this->translator->translator("member_loggin_redirecting");
Eicra_Global_Variable::getSession()->returnLink = '';
$this->logindetails->username->setAttrib('class', 'icon user-icon');
$this->logindetails->password->setAttrib('class', 'icon pass-icon');
?>
<script type="text/javascript">
$(document).ready(function() {

	$('a.reset_btn').click(function() {			
		 resetFields();	
		 removeError();	
	});
	
	function openMsgDialog(msg)
	{
		$('#dialog_msg').html(msg).dialog({
			autoOpen: true,
			modal: true,
			show: 'fade',
			resizable: 'false',				
			buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
			open: function(event, ui)
				 {
					setTimeout(function() 
					{									
						if($('#' + event.target.id).dialog('isOpen'))
						{
							$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
						}
					}, 10000);
				  }
		});
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	
	function resetFields()
	{
		$( "#AdminLogin" )[ 0 ].reset();
	}
	
	function removeError()
	{
		$('#actionMessage').html('');
				
		$.each($('#AdminLogin').serializeArray(), function(i, field) {				
				$('#'+field.name).removeClass('ui-state-error');
				$('#'+field.name+'_err').html('');									
		});			
	}
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');
		$('#'+err_id).append(errMsg);
	}
	
	function getFormData()
	{
		var fieldObj = {};
		$.each($('#AdminLogin').serializeArray(), function(i, field) {		 					
				fieldObj[field.name] = field.value;										
		});
		return fieldObj;
	}
	
	$('input.logout_btn').click(function() {
		
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Members','controller' => 'frontend', 'action'     => 'logout' ), 'default',    false) ?>",
			  	type: 'POST',
				beforeSend: function(){						
						removeError();
						openLoaderDialog();				
					 },
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");	
														
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));	
						$('#login_box').css('display','');	
						$('#logout_box').css('display','none');																									
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
					closeLoaderDialog();
					refreshCaptcha(json_arr);	
				}
		});
	});
	
	$('input.login_send_btn').click(function() {
		removeError();
		openLoaderDialog();
		var formData = getFormData();
		
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Members','controller' => 'frontend', 'action'     => 'auth' ), 'default',    false) ?>",
			  	type: 'POST',
			  	data: formData,
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");	
					refreshCaptcha(json_arr);									
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));	
						resetFields();
						var mem_link = (json_arr.role_id > 102) ? 'Members/index/profile' : 'Members-Login';
						$('#mem_link').attr('href',mem_link);	
						$('#login_box').css('display','none');	
						$('#logout_box').css('display','');	
						<?php if(!empty($returnLink)){	?>
							$('#actionMessage').html(succMsgDesign(json_arr.msg+'<?php echo $redirect_msg; ?>'));	
							document.location.href='<?php echo $returnLink;  ?>';
						<?php } ?>															
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
					closeLoaderDialog();
					
				}
		});
	});

	$('input.login_send_btn').keypress(function() {
		removeError();
		openLoaderDialog();
		var formData = getFormData();
		
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Members','controller' => 'frontend', 'action'     => 'auth' ), 'default',    false) ?>",
			  	type: 'POST',
			  	data: formData,
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");
					refreshCaptcha(json_arr);										
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));	
						resetFields();
						var mem_link = (json_arr.role_id > 102) ? 'Members/index/profile' : 'Members-Login';
						$('#mem_link').attr('href',mem_link);	
						$('#login_box').css('display','none');	
						$('#logout_box').css('display','');	
						<?php if(!empty($returnLink)){	?>
							$('#actionMessage').html(json_arr.msg+'<?php echo $redirect_msg; ?>');	
							document.location.href='<?php echo $returnLink;  ?>';
						<?php } ?>															
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
					closeLoaderDialog();
				}
		});
	});
	
	
	function refreshCaptcha(json_arr)
	{
		if(json_arr.captcha && json_arr.captcha.status && json_arr.captcha.status == 'ok')
		{
			var captcha_name = json_arr.captcha.captcha_name;
			$('input[name="'+captcha_name+'[id]"]').prev('img').attr('src', json_arr.captcha.ImgUrl+json_arr.captcha.id+json_arr.captcha.Suffix);
			$('input[name="'+captcha_name+'[id]"]').val(json_arr.captcha.id);
			$('#'+captcha_name+'_err').html('');		
		}
	}
	
	$('a.refresh_token').click(function() {		
		var self = this;
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Members','controller' => 'frontend', 'action'     => 'token' ), 'default',    false) ?>",
			  	type: 'POST',
				beforeSend: function(){	
						var option = '<img src="data/adminImages/loginImages/token_ani.gif" border="0" width="24" height="24"  title="Refreshing Token" />';						
						$(self).html(option);			
					 },
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");										
					if(json_arr.status == 'ok')
					{
						$('#token').val(json_arr.token);
						$(self).html('<img src="data/adminImages/loginImages/token.png" border="0" width="24" height="24"  title="Click To Refresh Token" />');
					}
				}
		});
	});	
		
});
</script>
<div class="mod mod-login">
	<div class="login-box">
       	<h1><?php echo $this->translator->translator('Member_frontend_login_title'); ?></h1>
		<hr />
        <div id="actionMessage"><?php echo $this->logindetails->getDescription(); ?></div>
        <div class="login-img"></div>
        <div id="login_box" class="field-area" style="display:<?php if(Zend_Auth::getInstance()->hasIdentity()){	?>none;<?php }?>">
            <form id="AdminLogin" name="AdminLogin" method="post" action="">                    
                <p class="label"><?php  echo $this->logindetails->username->getLabel(); ?></p>
                <?php  echo $this->logindetails->username;?>
                <span class="input-errors" id="username_err"></span>
                
                <p class="label"><?php  echo $this->logindetails->password->getLabel(); ?></p>
                <?php  echo $this->logindetails->password;?>
                <span class="input-errors" id="password_err"></span>
                <br />
                <?php if($this->logindetails->login_captcha){ ?>
                <p class="label"><?php  echo $this->logindetails->login_captcha->getLabel();	?></p><br />
                <div class="captcha">
                    <?php  echo $this->logindetails->login_captcha;?>
                    <div class="form-field-info"><div class="ui-widget ui-helper-clearfix"><div class="ui-state-default ui-corner-all info-class" title="<?php echo $this->escape($this->logindetails->login_captcha->getAttrib('info')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div></div>
                    <span class="input-errors" id="login_captcha_err"></span>
                </div>
                <?php } ?>
                <?php echo $this->logindetails->token; ?>
                
                <div class="clear"><input type="button" name="login" value="<?php echo $this->translator->translator('Member_frontend_login_button'); ?>" class="login_send_btn"/></div>
               
                <span class="signup"><a href="Register" class="reg"><?php echo $this->translator->translator('Member_frontend_register_link'); ?></a> &nbsp;|&nbsp; <a href="Forget-Password"><?php echo $this->translator->translator('Member_frontend_forget_pass_link'); ?></a></span>
            </form>
        </div>
        
        <div id="logout_box" style="display:<?php if(!Zend_Auth::getInstance()->hasIdentity()){	?>none;<?php }?>">
            <input type="button" name="login" value="<?php echo $this->translator->translator('Member_frontend_logout_button'); ?>" class="logout-btn logout_btn" />
            <?php $globalIdentity = Zend_Auth::getInstance()->getIdentity (); $mem_link = ($globalIdentity->role_id > 102) ? 'Members/index/profile' : 'Members-Login'; ?>                
           	<div><a href="<?php echo $mem_link; ?>"  id="mem_link"><?php echo $this->translator->translator('Member_frontend_login_area'); ?></a> | <a href="Property/backendpro/add"  id="mem_link"><?php echo $this->translator->translator('Member_frontend_add_your_maid_now'); ?></a> | <a href=""  id="mem_link"><?php echo $this->translator->translator('Member_frontend_home'); ?></a></div>
        </div> 
    </div>
</div>

<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg','Message'); ?>"></div>
<div id="dialog_container" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar" ></div>	
</div>